package com.example.pertecttime.activity.login;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import com.example.pertecttime.R;
import com.example.pertecttime.base.BaseActicvity;
import com.example.pertecttime.entity.User;
import com.example.pertecttime.tools.JumpUtil;

public class loginActivity extends BaseActicvity<loginPresenter, LoginModel> implements loginContract.View {


    private EditText lg_username_ed;

    private EditText lg_password_ed;

    private CheckBox lg_Rememberpassword_cb;

    private CheckBox lg_antologin_cb;

    private Button lg_dologin_bt;


    @Override
    public int getLayoutId() {
        return R.layout.loginlayout;
    }

    @Override
    public void initview() {

        lg_dologin_bt = (Button) findViewById(R.id.lg_loginButton_bt);
        lg_username_ed = (EditText) findViewById(R.id.lg_username_ed);
        lg_password_ed = (EditText) findViewById(R.id.lg_password_ed);
        lg_antologin_cb = (CheckBox) findViewById(R.id.lg_antologin_cb);
        lg_Rememberpassword_cb = (CheckBox) findViewById(R.id.lg_Rememberpassword_cb);
        lg_dologin_bt.setOnClickListener(this);
        lg_antologin_cb.setOnCheckedChangeListener(cb);
        lg_Rememberpassword_cb.setOnCheckedChangeListener(cd);
    }

    @Override
    public void init() {
        presenter.onCreate(this);

        //todo test
    }


    @Override
    public void loginSuccess(User user) {

        t.show_s("成功");
//        presenter.savaRecord(lg_Rememberpassword_cb.isChecked(), lg_antologin_cb.isChecked(), lg_username_ed.getText().toString().trim(), lg_password_ed.getText().toString().trim());
        JumpUtil.toMain(this);
    }

    @Override
    public void loginFail(int s) {
        t.show_s(String.valueOf(s));
    }

    @Override
    public void onCheckBoxResult(boolean remembercode, boolean antucode, String username, String password) {

        lg_Rememberpassword_cb.setChecked(remembercode);
        lg_antologin_cb.setChecked(antucode);
        lg_username_ed.setText(username);
        lg_password_ed.setText(password);

    }

    @Override
    protected void processClick(View view) {
        JumpUtil.toMain(this);
//        switch (view.getId()) {
//
//            case R.id.lg_loginButton_bt:
//                String username, password;
//                username = lg_username_ed.getText().toString().trim();
//                password = lg_password_ed.getText().toString().trim();
//                if (username.length() == 0 && password.length() == 0) {
//                    t.show_s("账号与密码不能为空");
//                } else {
//                    presenter.dologin(lg_Rememberpassword_cb.isChecked(), lg_antologin_cb.isChecked(), username, password);
//                }
//                break;
//        }


    }

    private CompoundButton.OnCheckedChangeListener cb = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (b) {
                if (b == true) {

                    lg_Rememberpassword_cb.setChecked(true);

                }
            }
        }
    };
    private CompoundButton.OnCheckedChangeListener cd = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (b == false) {

                lg_antologin_cb.setChecked(false);

            }
        }
    };

    public Context getContext() {
        return this;
    }
}
